package com.flc.archive.mq.listener;

import com.flc.archive.Archive;
import com.flc.gateway.comm.data.DiagnosisData;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.io.IOException;
import java.util.List;

/**
 * @author haizi
 * @date 2018/1/31
 */
public class DiagnosisListener implements MessageListenerConcurrently {

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        // 每次只能收到一条消息，并不是批量的
        DiagnosisData diagnosisData = new DiagnosisData();
        diagnosisData.setData(msgs.get(0).getBody());
        String dataStr = diagnosisData.getDataStr();
        try {
            Archive.diagnosisWriter.write(dataStr);
        } catch (IOException e) {
            e.printStackTrace();
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

}
